package com.jprocms.module.cms.service.content;

import cn.hutool.core.lang.Assert;
import com.jprocms.module.cms.dal.dataobject.content.ContentTypeDO;
import com.jprocms.module.cms.dal.mysql.content.ContentTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.COMMON_PARAM_ERROR;

/**
 * 内容类型 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class ContentTypeServiceImpl implements ContentTypeService {

    @Resource
    private ContentTypeMapper contentTypeMapper;

    /**
     * 保存内容类型
     *
     * @param typeIds 内容类型id
     * @return contentId 内容id
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveContentType(Long contentId, List<Long> typeIds) {
        Assert.notNull(contentId, () -> {
            throw exception(COMMON_PARAM_ERROR);
        });
        contentTypeMapper.delete(ContentTypeDO::getContentId, contentId);
        List<ContentTypeDO> contentTypeDOS = new ArrayList<>();
        typeIds.forEach(typeId -> {
            ContentTypeDO tagDo = new ContentTypeDO(contentId, typeId);
            contentTypeDOS.add(tagDo);
        });
        contentTypeMapper.insertBatch(contentTypeDOS);
    }
}
